package snippet.jms;

import java.util.Properties;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class HelloQueueReceiver
{

	static final String providerContextFactoryTIBCO = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";

	static final String providerURLTIBCO = "tibjmsnaming://localhost:7222";

	static final String providerContextFactoryActiveMQ = "org.apache.activemq.jndi.ActiveMQInitialContextFactory";

	static final String providerURLActiveMQ = "tcp://localhost:61696";

	public static void main(final String[] args)
	{
		try
		{
			// Locate the JNDI naming service
			final Context ctx = getInitialContext();

			// Locate the Queue Connection Factory via JNDI
			final QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");

			// Create a new Queue Connection
			final QueueConnection conn = factory.createQueueConnection();

			// Create a Queue Session, ask JMS to acknowledge the messages
			// This program receives messages, so it doesn't really care.
			// The session is non-transactional 

			final QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);


			Queue queue = null;
			try
			{
				// See if someone has already created the queue
				//queue = (Queue) ctx.lookup("helloQueue");
				queue = session.createQueue("helloQueue");
			}
			catch (final Exception exc)
			{

				System.out.println("Queue not created");
				// If not, create a new Queue and store it in the JNDI directory
				//queue = session.createQueue("helloQueue");
				ctx.bind("helloQueue", queue);
			}

			// Create a QueueReceiver to receive messages
			final QueueReceiver receiver = session.createReceiver(queue);

			// Tell the Queue Connection you are ready to interact with the message service
			conn.start();

			int drain_count = 0;
			System.err.println("\nDraining the queue " + queue);

			/* read queue until empty */
			//            while (receiver.receive(10) != null) // v 1.0 wudnt work with AMQ , but works with TIBCO EMS
			//          while (receiver.receiveNoWait() != null) // v 2.0 wudnt work with AMQ , but works with TIBCO EMS

			while (receiver.receive(100) != null) // v 3.0 timeout big enough fr both AMQ and TIBCO EMS
			{
				drain_count++;
			}
			System.err.println("Finished draining " + drain_count + " messages from the queue");

			//for (;;)
			//{
			// Receive the next message
			//                TextMessage message = (TextMessage) receiver.receive(1000);
			//            Integer.parseInt(args[0])
			//            javax.jms.Message message = receiver.receive(30000);

			// Print the message contents
			//if(message != null)
			//            		System.out.println(message);
			//}
			conn.close();
		}
		catch (final Exception exc)
		{
			exc.printStackTrace();
		}
	}

	private static InitialContext getInitialContext() throws NamingException
	{
		final Properties prop = new Properties();
		prop.put("java.naming.factory.initial", providerContextFactoryTIBCO);
		prop.put("java.naming.provider.url", providerURLTIBCO);
		//prop.put("queue.helloQueue", "helloQueue");
		//prop.put("queue.sampleQueue", "sampleQueue");

		return new InitialContext(prop);
	}
}
